# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  include TagsHelper
  
  def title_suffix
    "&middot;&nbsp; &middot;&nbsp; &middot;&nbsp; &middot;&nbsp; &middot;&nbsp; &middot;&nbsp;"
  end
  
  def error_msg(object, error, msg)
    return "<span class=\"errorExplan\">#{msg}</span>" if object && object.errors[error]
  end
  
  def avatar_for(user, size = "")
    user.avatar_url.blank? ? "<img src=\"/images/avatar_#{size}.png\" class=\"avatar\" alt=\"#{user.login}\" />" : image_tag(url_for_file_column(user, "avatar_url", size), :class => "avatar", :alt => user.login)
  end
  
  #TODO refactor, this method is different from dizhen/base_helper.rb
=begin  
  def nav_menu(text, url, controller_name, action_name=nil)
    if @controller.controller_name == controller_name && (action_name.nil? || @controller.action_name == action_name)
      return link_to(text, url, :class => "now")
    else
      return link_to(text, url)
    end
  end
=end
  def nav_menu(text, url, session_name)
    if session[:nav] == session_name
      return link_to(text, url, :class => "now")
    else
      return link_to(text, url)
    end
  end
  
  def areas_crumble_path_with_link(area)
    result = ""
    area.ancestors.each do |p| 
      result += yield(p) + " > "
    end
    result += area.title
  end

  def show_date(datetime)
    "#{datetime.year}-#{datetime.month}-#{datetime.day}"
  end
  
  def show_datetime(datetime, timezone_ajust=true, with_weekday = true)
    datetime += 13.hour if timezone_ajust
    wday = %w(星期日 星期一 星期二 星期三 星期四 星期五 星期六)
    if with_weekday
      show_date(datetime) + "&nbsp;#{wday[datetime.wday]}" + "&nbsp;#{sprintf('%.2d', datetime.hour)}:#{sprintf('%.2d',datetime.min)}"
    else
      show_date(datetime) + "&nbsp;#{sprintf('%.2d', datetime.hour)}:#{sprintf('%.2d',datetime.min)}"
    end
  end
  
  def inbox_title(unread_amount)
    if unread_amount.blank? || unread_amount == 0
      return "我的消息"
    else
      return "<strong>我的消息(#{unread_amount})</strong>"
    end
  end
  
  def link_to_school_page(school, opts={})
    options = {:title => "查看#{h(school.title)}的信息"}
    options.merge!(opts)
    link_to h(school.title), school_url(school.id), options
  end
  
  def link_to_event_page(event, opts={})
    options = {:title => "查看#{h(event.title)}的信息"}
    options.merge!(opts)
    link_to h(event.title), event_url(event.id), options
  end
  
  def link_to_group_topic_page(topic, opts={})
    options = {:title => topic.title}
    options.merge!(opts)
    link_to h(topic.title), topic_url(topic.group.id, topic.id), options
  end
  
  def link_to_user_page(user, opts={})
    options = {:title => "去#{h(user.login)}的主页看看"}
    options.merge!(opts)
    link_to h(user.login), user_url(user.id), options
  end
  
  # ajax链接
  def link_to_r(label, options = {})
    link_to_remote(label,:url => options, :method => :get)
  end
  
  def calc_time_to_now(from_time, to_time = Time.now)
    original_time = from_time
    from_time = from_time.to_time if from_time.respond_to?(:to_time)
    to_time		= to_time.to_time		if to_time.respond_to?(:to_time)
    distance_in_minutes = (((to_time - from_time).abs)/60).round

    case distance_in_minutes
      when 0..1             then "1 分钟前"
      when 2..44						then "#{distance_in_minutes} 分钟前"
      when 45..89						then "1 小时前"
      when 90..1439					then "#{(distance_in_minutes.to_f / 60.0).round} 小时前"
      when 1440..2879				then "1 天前"
      when 2880..43199			then "#{(distance_in_minutes / 1440).round} 天前"
      when 43200..86399			then "1 个月前"
    else
      show_date(original_time)
    end
  end
end
